package com.example.lib.extend

import android.app.Activity
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.lib.utils.glide.GlideCircleTransform

/**
 * 加载普通图片
 */
fun ImageView.loadImage(path: Any, error: Int = 0) {
    if (context is Activity) {
        if ((context as Activity).isFinishing || (context as Activity).isDestroyed)
            return
    }
    try {
        var load = Glide.with(context).load(path)
        if (error != 0) {
            load = load.placeholder(error)
            load = load.error(error)
        }
        load.into(this)
    } catch (e: Exception) {

    }
}

/**
 * 加载圆角图片
 */
fun ImageView.loadRadiusImage(path: Any, radius: Int, error: Int = 0) {
    if (context is Activity) {
        if ((context as Activity).isFinishing || (context as Activity).isDestroyed)
            return
    }
    try {
        var load = Glide.with(context).load(path)
        if (error != 0) {
            load = load.placeholder(error)
            load = load.error(error)
        }
        load =
            load.apply(RequestOptions.bitmapTransform(RoundedCorners(context.dip2px(radius.toFloat()))))
        load.into(this)
    } catch (e: Exception) {

    }
}

fun ImageView.loadGif(path: Any, error: Int = 0) {
    if (context is Activity) {
        if ((context as Activity).isFinishing || (context as Activity).isDestroyed)
            return
    }
    try {
        var load = Glide.with(context).asGif().load(path)
        if (error != 0) {
            load = load.placeholder(error)
            load = load.error(error)
        }
        load.into(this)
    } catch (e: Exception) {

    }
}

/**
 * 加载圆形图片
 */
fun ImageView.loadCircleImage(path: Any, placeholder: Int = 0, error: Int = 0) {
    if (context is Activity) {
        if ((context as Activity).isFinishing || (context as Activity).isDestroyed)
            return
    }
    try {
        var load = Glide.with(context).load(path)
        if (placeholder != 0) {
            load = load.placeholder(placeholder)
        }
        if (error != 0) {
            load = load.error(error)
        }
        load = load.apply(RequestOptions.bitmapTransform(GlideCircleTransform()))
        load.into(this)
    } catch (e: Exception) {

    }
}

